const path = require("path");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");

module.exports = {
  // 打包模式
  mode: "development",

  // 入口配置
  //    chunk
  entry: "./src/index.js",

  // 出口配置
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "./dist"),
    // 是否清空构建目录
    clean: true,
  },

  // module.rules 配置 loader 规则
  module: {
    rules: [
      {
        test: /\.css$/,
        // use: ["style-loader", "css-loader"],
        // 配置 MiniCssExtractPlugin
        use: [MiniCssExtractPlugin.loader, "css-loader"],
      },
    ],
  },

  // 插件配置
  plugins: [
    new HtmlWebpackPlugin({
      // 让script标签插入到 body 下
      inject: "body",
      // 指定一个生成html的模板页面
      template: "./public/index.html",
    }),
    new CopyWebpackPlugin({
      patterns: [
        {
          // 指定要拷贝的文件或目录
          from: path.resolve(__dirname, "./public"),
          globOptions: {
            // 配置要排除的文件集合
            ignore: ["**/*.html"],
          },
        },
      ],
    }),
    new MiniCssExtractPlugin(),
  ],
};
